// 03myCenter/01myCenter/myCenter.js
var co = require('../../resource/js/common.js')
var M = require('../../resource/js/md5.js')
var app = getApp()
var _this
var interval = null //倒计时函数

Page({
    /*页面的初始数据*/
    data: {
        items: '',
        userInfo: "",
        animation: '',
        //切换登录方式
        isThis: 0,
        // 登录方式错误提示信息
        isUserName: '',
        isPassWord: '',
        isPhoneNumber: '',
        isVerificationCode: '',
        // 短信验证码
        time: '获取验证码',
        currentTime: 120,
        smsCode: '',
        // 输入的手机号
        phoneNumber: '',
        tempPhone: '',      
        //自定义弹窗参数
        setGuestNumShow: 0,
        numArray: [
            [1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 11, 12],
            [13, 14, 15, 0]
        ],
        // 传递信息
        Biz_ID: '',
        Table_ID: '',
        timeID: ''
    },
    /*生命周期函数--监听页面加载*/
    onLoad: function (options) {
        _this = this
        this.getUserSetItems()
        this.setData({
            userInfo: app.Data.userInfo
        })
    },
    // 返回刷新页面
    onShow: function(){
       this.onLoad()
        // var timeID = setInterval(function () {
        //     var i = 5
        //     _this.rotateAni(i)
        // }, 500)
    },
    // 头像旋转动画
    // rotateAni: function (n) {
    //     this.animation = wx.createAnimation({
    //         duration: 500,
    //         timingFunction: 'linear',
    //         delay: 0,
    //     })
    //     this.animation.rotate(n).step()
    //     this.setData({
    //         animation: this.animation.export()
    //     })
    // },
    // 获取个人中心列表数据显示隐藏
    getUserSetItems() {
        var param = {
            Perm_Tyle: 2
        }
        co.getMealApi('menu/Config/getMenuList', param, function (res) {
            if (res.code == 0) {
                _this.setData({ items: res.data })
            }
        })
    },
    // 点击进入页面
    toNextUrl: function(e){
        if (e.currentTarget.id == ''){
            this.scanCode()
        }else{
            wx.navigateTo({
                url: e.currentTarget.id,
            }) 
        }
    },
    // 再次调用微信授权
    getWxOpenInfo: function (e) {
        //   console.log("【获取用户信息】===", e.detail)
        var param = {
            appid: app.Data.app.appid,
            session_key: wx.getStorageSync('session_key'),
            iv: e.detail.iv,
            encryptedData: e.detail.encryptedData
        }
        co.getShareApi('user/Mg/getInfo', param, function (res) {
            if (res.code == 0) {
                app.Data.userInfo = res.data
                // 授权登录刷新userInfo
                _this.setData({
                    userInfo: res.data
                })
            } else if (res.code == 93748) {
                app.wxLogin()   //解密失败可能是session_key失效了，需重新登录获取session_key
            }
        })
    },
    // 获取手机号
    getPhoneNumber: function (e) {
        var phone = e.detail.value             //输入的手机号码存入
        this.setData({
            phoneNumber: phone
        })
    },
    // 获取验证码
    getVerCode: function () {
        var inPhone = this.data.phoneNumber       //输入手机号
        var telRule = /^1[3|4|5|7|8]\d{9}$/       //手机号码格式
        var isPhoneNumber = ''
        if (inPhone == '') {
            co.toast("请输入手机号码!")
        } else if (!telRule.test(inPhone)) {
            isPhoneNumber = '*手机号码格式错误*'
        } else {
            this.getCode();
            this.setData({
                disabled: true
            })
            var param = {
                Phone_No: this.data.phoneNumber
            }
            co.getShareApi('api/Sms/getPhoneCode', param, function (res) {
                console.log("【获取返回数据】===", res.data)
                _this.setData({
                    smsCode: res.data.code
                })
            })
        }
        this.setData({
            isPhoneNumber: isPhoneNumber,
            tempPhone: inPhone,
        })
    },
    // 确认登录
    confirmLogin: function (e) {
        // console.log("【form携带值为】===", e.detail.value)
        var et = e.detail.value             //form携带值为
        var id = this.data.isThis           //登录方式id
        var smsCode = this.data.smsCode     //获取到的短信验证码
        var tmPhone = this.data.tempPhone   //获取验证码的手机码
        var USN = et.userName               //form提交用户名
        var PSW = et.passWord               //form提交密码
        var PHN = et.phoneNumber            //from提交手机号
        var VCO = et.verificationCode       //from提交验证码
        var isUserName = ''
        var isPassWord = ''
        var isPhoneNumber = ''
        var isVerificationCode = ''
        if (id == 0) {
            if (USN == '') {
                isUserName = '*用户名必填*'
            }
            if (PSW == '') {
                isPassWord = '*密码必填*'
            }
            var param = {
                User_Mobile: USN,
                User_Password: M.md5(PSW)
            }
            this.Login(param)
        } else {
            if (PHN == '') {
                isPhoneNumber = '*手机号码必填*'
            }
            if (VCO == '') {
                isVerificationCode = '*验证码必填*'
            }
            // 判断验证码是否相等
            if (VCO != smsCode) {
                isVerificationCode = '*验证码错误*'
            } else if (tmPhone != PHN) {
                isVerificationCode = '*验证码已过期*'
            } else {
                var param = {
                    User_Mobile: PHN,
                    Sms_Code: VCO
                }
                this.Login(param)
            }
        }
        this.setData({
            isUserName: isUserName,
            isPassWord: isPassWord,
            isPhoneNumber: isPhoneNumber,
            isVerificationCode: isVerificationCode
        })
    },
    // 登录
    Login: function(param){
        var isUserName = ''
        var isPassWord = ''
        var isPhoneNumber = ''
        // 账号密码登录
        co.getShareApi('user/Fmg/userLogin', param, function (res) {
            // console.log("【登录成功返回数据】===",res.data)
            if (res.code == 29300) {
                isUserName = res.msg
            }
            if (res.code == 29375) {
                isPassWord = res.msg
            }
            if (res.code == 0) {
                _this.setData({
                    userInfo: res.data
                })
            }
            _this.setData({
                isUserName: isUserName,
                isPassWord: isPassWord,
                isPhoneNumber: isUserName
            })
            console.log("【data】===", _this.data)
        })
    },
    // 点击切换登录
    changeContent: function (e) {
        var id = e.currentTarget.id
        if (id == '0') {
            this.setData({ isThis: 0 })
        } else {
            this.setData({ isThis: 1 })
        }
    },
    /*获取短信验证码倒计时*/
    getCode: function (options) {
        var currentTime = this.data.currentTime
        interval = setInterval(function () {
            currentTime--;
            _this.setData({
                time: currentTime + '秒'
            })
            if (currentTime <= 0) {
                clearInterval(interval)
                _this.setData({
                    time: '重新发送',
                    currentTime: 120,
                    disabled: false,
                    smsCode: '',
                })
            }
        }, 1000)
    },
    // 扫描点菜
    scanCode: function () {
        wx.scanCode({
            onlyFromCamera: true,
            success: function (res) {
                var str = res.result
                if (str.indexOf('meal.52g3.cc/index/index/index') > -1) {
                    var strArr = str.split('/')
                    // console.log("【获取】===", strArr, strArr[9], strArr[7])
                    _this.getTableInfo(strArr[9], strArr[7])
                    _this.setData({
                        Biz_ID: strArr[9],
                        Table_ID: strArr[7]
                    })
                } else {
                    co.toast('您扫的不是码上点的二维码')
                }
            }
        })
    },
    //获取桌号对应订单信息
    getTableInfo: function (biz_id, tableId) {
        var param = {
            Biz_ID: biz_id,
            Table_ID: tableId
        }
        co.getShareApi('order/Omg/tableOrder', param, function (res) {
            console.log("【获取返回信息】===", res.data)
            if (res.code == 0) {//此桌有订单时
                var obj = JSON.stringify(res.data)
                wx.navigateTo({
                    url: '/02local/07tableOrder/tableOrder?obj=' + obj,
                })
            } else if (res.code == 29353) {//此桌无订单时
                wx.navigateTo({
                    url: '/02local/03foodInfo/foodInfo?Biz_ID=' + _this.data.Biz_ID + '&Table_ID=' + _this.data.Table_ID
                })
            }
        })
    }
})